<?php

namespace App\Listeners;

use App\Events\CloudEvent;
use App\Events\Event;
use App\Model\System\Organization;
use Exception;

class CloudListener extends  Listener
{

    /**
     * @param CloudEvent $event
     */
    public function handle(CloudEvent $event)
    {
        switch($event->action)
        {
            case CloudEvent::INIT:
                $this->init($event);
                break;

            case CloudEvent::MAKE:
                $this->make($event);
                break;

            case CloudEvent::RENAME:
                $this->rename($event);
                break;

            case CloudEvent::REMOVE:
                $this->remove($event);
                break;

            default:
                $this->read($event);
        }
    }

    /**
     * 获取文件列表
     * @param CloudEvent $event
     */
    protected function read(CloudEvent $event)
    {
        try{
            if($event->id === 0){
                $this->setCloudUri('getFolderList');
                $folderId = '';
            }
            else {
                $this->setCloudUri('getCloudList');
                $folderId = $event->id;
                if ($event->id === '0')
                    $folderId = $this->getRootid();
            }
            $data = $this->dataMaker();
            $data["folderId"] = $folderId;
            $this->setRequest($data);
            $result = $this->callEeoApi();
            if($result['error_info']['errno'] === 1) {
                if(key_exists('data',$result))
                    $event->setResult($result['data']);
                else{
                    $data = ['folder_list' => [], 'file_list' => []];
                    if(key_exists('folder_list',$result)){
                        $data['folder_list'] = $result['folder_list'];
                    }
                    if(key_exists('file_list',$result)){
                        $data['file_list'] = $result['file_list'];
                    }
                    $extdata = $this->checkFolder($folderId);
                    $data['folder_id'] = $extdata['folder_id'];
                    $data['folder_name'] = $extdata['folder_name'];
                    $event->setResult($data);
                }
            }else{
                throw new Exception($result['error_info']['error'], $result['error_info']['errno']);
            }
        }catch (Exception $e){
            throw $e;
        }

    }

    /**
     * 上传文件/文件夹
     * @param $event
     */
    protected function make(CloudEvent $event)
    {
        try{
            if($event->type === 1) {
                $this->setCloudUri('uploadFile');
                $data = $this->dataMaker();
                $data["folderId"] = $event->pid;
                if($event->pid == 0)
                    $data["folderId"] = $this->getRootid();
                $data["Filedata"] = $this->uploadCheck($event->Filedata);
            }
            else{
                $this->setCloudUri('createFolder');
                $data = $this->dataMaker();
                $data["folderId"] = $event->pid;
                if($event->pid == 0)
                    $data["folderId"] = $this->getRootid();
                $data["folderName"] = $event->name;
            }
            $this->setRequest($data);
            $result = $this->callEeoApi();
            if($result['error_info']['errno'] === 1) {
                $event->setResult($result['data']);
            }elseif($result['error_info']['errno'] === 206) {
                $uid = substr(md5(time()),0,5);
                $event->name = $event->name.'('.$uid.')';
                $this->make($event);
            }else{
                throw new Exception($result['error_info']['error'], $result['error_info']['errno']);
            }
        }catch (Exception $e){
            throw $e;
        }

    }


    /**
     * 重命名文件/文件夹
     * @param $event
     */
    protected function rename(CloudEvent $event)
    {
        try{
            if($event->type === 1) {
                $this->setCloudUri('renameFile');
                $data = $this->dataMaker();
                $data["fileId"] = $event->id;
                $data["fileName"] = $event->name;
            }
            else{
                $this->setCloudUri('renameFolder');
                $data = $this->dataMaker();
                $data["folderId"] = $event->id;
                $data["folderName"] = $event->name;
            }
            $this->setRequest($data);
            $result = $this->callEeoApi();
            if($result['error_info']['errno'] <> 1) {
                throw new Exception($result['error_info']['error'], $result['error_info']['errno']);
            }
        }catch (Exception $e){
            throw $e;
        }

    }


    /**
     * 删除文件/文件夹
     * @param $event
     */
    protected function remove(CloudEvent $event)
    {
        try{
            if($event->type === 1) {
                $this->setCloudUri('delFile');
                $data = $this->dataMaker();
                $data["fileId"] = $event->id;
            }
            else{
                $this->setCloudUri('delFolder');
                $data = $this->dataMaker();
                $data["folderId"] = $event->id;
            }
            $this->setRequest($data);
            $result = $this->callEeoApi();
            if($result['error_info']['errno'] <> 1){
                throw new Exception($result['error_info']['error'], $result['error_info']['errno']);
            }
        }catch (Exception $e){
            throw $e;
        }

    }

    /**
     * 初始化机构云盘根目录
     * @param CloudEvent $event
     * @throws Exception
     */
    protected function init($event)
    {

        try{
            $rootid = 0;
            $this->setCloudUri('getTopFolderId');
            $data = $this->dataMaker();
            $this->setRequest($data);
            $result = $this->callEeoApi();
            if($result['error_info']['errno'] == 1) {
                $rootid = $result['data'];
                $obj = Organization::find(1);
                $obj->rootid = $rootid;
                $obj->save();
            }else{
                $rootid = 0;
                $obj = Organization::find(1);
                $obj->rootid = $rootid;
                $obj->save();
                throw new Exception($result['error_info']['error'], $result['error_info']['errno']);
            }
        }catch (Exception $e){
            throw $e;
        }
    }

    /**
     * 获取文件夹ID及名称
     * @param $folderId
     */
    protected function checkFolder($folderId){
        try{
            $this->setCloudUri('getFolderList');
            $data = $this->dataMaker();
            $this->setRequest($data);
            $result = $this->callEeoApi();
            if($result['error_info']['errno'] === 1) {
                foreach ($result['data'] as $ids){
                    foreach ($ids as $id){
                        if($id['id'] == $folderId){
                            $extdata['folder_name'] = $id['name'];
                            $extdata['folder_id'] = ''.$folderId;
                            return $extdata;
                        }
                    }
                }

            }else{
                throw new Exception($result['error_info']['error'], $result['error_info']['errno']);
            }
        }catch (Exception $e){
            throw $e;
        }
    }


}
